package com.xiaoyi.tool;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SharePreferenceUtil {
    private final SharedPreferences sharedpreferences;
    private final int MODE = Context.MODE_PRIVATE;
    Context context;

    /**
     * 第一个参数：存储时的名字 第二个参数：文件的打开方式
     * mode指定为MODE_PRIVATE，则该配置文件只能被自己的应用程序访问。（默认为MODE_PRIVATE ）
     * mode指定为MODE_WORLD_READABLE，则该配置文件除了自己访问外还可以被其它应该程序读取。
     * mode指定为MODE_WORLD_WRITEABLE，则该配置文件除了自己访问外还可以被其它应该程序读取和写入
     */
    public SharePreferenceUtil(Context context, String fileName) {
        // MOD
        sharedpreferences = context.getSharedPreferences(fileName, MODE);
        this.context = context;
    }

    /**
     * 保存键值
     */
    public boolean saveSharedPreferences(String key, String value) {
        Editor editor = sharedpreferences.edit();
        editor.putString(key, value);
        boolean flag = editor.commit();
        return flag;

    }

    /**
     * @param key   String
     * @param value int
     * @return boolean
     */
    public boolean saveSharePresferenceTypeOfInt(String key, int value) {
        Editor editor = sharedpreferences.edit();
        editor.putInt(key, value);
        boolean flag = editor.commit();
        return flag;
    }

    /**
     * 得到值
     */
    public String getStringByKey(String key) {
        String str = "http://192.168.1.1:8080/";
        try {
            str = sharedpreferences.getString(key, "http://192.168.1.1:8080/");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public int getIntByKey(String key) {
        return sharedpreferences.getInt(key, 0);
    }

    /**
     * 删除值
     */
    public void logoutSharePreference() {
        if (sharedpreferences != null) {
            Editor editor = sharedpreferences.edit();
            editor.clear();
            editor.commit();
        }

    }

    /**
     * 保存对象
     */
    public void saveObject(String objectKeyStr, Object object) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            String base64Student = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
            Editor editor = sharedpreferences.edit();
            editor.putString(objectKeyStr, base64Student);
            editor.apply();
            oos.close();
            editor.commit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }//

    /**
     * 取出对象
     */
    public Object getObject(String objectKeyStr) {
        Object object = null;
        String objectStr = sharedpreferences.getString(objectKeyStr, "N/A");
        byte[] base64Student = Base64.decode(objectStr, Base64.DEFAULT);
        ByteArrayInputStream bais = new ByteArrayInputStream(base64Student);
        try {
            ObjectInputStream ois = new ObjectInputStream(bais);
            object = ois.readObject();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }//


}